package com.atguigu.bigdata.spark.sql;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.*;
import org.apache.spark.sql.types.DataTypes;
/*
    spark sql 自定义函数
    自定义函数分为3种 :
    UDF(User-Defined-Function) : 一对一，类似to_char , to_date等
    UDAF(User-Defined Aggregation Funcation) : 多对一，类似在group by之后使用的sum , avg
    UDTF(User-Defined Table-Generating Functions) : 一对多，有点像stream里面的 flatMap
 */
public class Spark02_SparkSQL_UDF_JAVA {
    public static void main(String[] args) throws AnalysisException {
        SparkConf conf = new  SparkConf().setMaster("local[*]").setAppName("sparkSQL");
        SparkSession spark = SparkSession.builder().config(conf).getOrCreate();

        Dataset<Row> df   = spark.read().json("datas/user.json");

        df.createOrReplaceTempView("user");
        spark.udf().register("prefixName",(String name) ->
            "Name: " + name
        , DataTypes.StringType);
        spark.sql("select age, prefixName(username) from user").show();

        spark.close();

    }
}
